<?php 

require("../include/config.php");
require("../API/php/fonctions.php");
require("../API/php/header.php");

$modeDebug = false;

// phpsql/mysql_getDetailTicketsDefect.php?milis=123456789&filterDefect=D317,D325

//[X][0] = id_defect
//[X][1] = nb_tocs
//[X][2] = id_toc_ori
//[X][3] = responsable

// IN obligatoire
$arrayGet = array(
    "filterDefect" => null
);

$arrayValeur = recupGet($arrayGet, $bolDecode);

if($arrayValeur["error"] == null){
	// CONNECION SQL
	$db = mysql_connect($host, $base, $mdp)  or die('Erreur de connexion '.mysql_error());

	// CONNECION BASE
	mysql_select_db($base,$db)  or die('Erreur de selection '.mysql_error());
	
	//------------------------------------------------------------------
	//pourcent de saisi d activitee
	
	$sql = "SELECT null as id_defect, 0 as nb_tocs, id_toc_ori, responsable FROM `vm_inventaire_tocs` WHERE 1=0 ";
	
	if($arrayValeur["filterDefect"] != ""){
		$out = preg_split("[,]", $arrayValeur["filterDefect"]);
		
		foreach($out as $i => $v_id_defect)
		{
			$num_defect = substr($v_id_defect,1);
			$sql .= "UNION SELECT '".$v_id_defect."' as id_defect, (SELECT COUNT(*) FROM `vm_inventaire_tocs` a WHERE 1=1 AND a.`liaisons` REGEXP 'DEFECT_".$num_defect."') as nb_tocs, id_toc_ori, responsable FROM `vm_inventaire_tocs` a WHERE 1=1 AND a.`liaisons` REGEXP 'DEFECT_".$num_defect."' ";
		}
	}
		
	$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

	// on fait une boucle qui va faire un tour pour chaque enregistrement 
	while($data = mysql_fetch_assoc($req)) 
	{ 
		$strSorti .= $data['id_defect']."&".$data['nb_tocs']."&".$data['id_toc_ori']."&".$data['responsable']
		."&"
		."\r\n"; 
	}

	// DECONNECION SQL
	mysql_close();
	
	if($modeDebug){
		$strSorti .= ('<br><br><br><br>'.$sql);
	}
}else{
	$strSorti = "ERROR:".$arrayValeur["error"];
}

require("../API/php/footer.php");
?>